"""
预约相关 Schemas
"""
from pydantic import BaseModel, Field
from typing import Optional, List
from datetime import datetime
from app.schemas.common import Location


class BookingParticipantBase(BaseModel):
    """预约参与者基础信息"""
    userId: str
    userName: str
    avatar: Optional[str] = None
    position: str


class BookingParticipantResponse(BookingParticipantBase):
    """预约参与者响应"""
    confirmed: bool = False
    paid: bool = False
    paidAmount: float = 0.0
    paidAt: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class BookingBase(BaseModel):
    """预约基础信息"""
    type: str = Field(..., pattern="^(team|match)$")
    venueId: str
    venueName: str
    venueType: str
    date: str = Field(..., pattern=r"^\d{4}-\d{2}-\d{2}$")
    startTime: str = Field(..., pattern=r"^\d{2}:\d{2}$")
    endTime: str = Field(..., pattern=r"^\d{2}:\d{2}$")
    venuePrice: float
    serviceFee: float = 0.0
    totalPrice: float


class BookingCreate(BookingBase):
    """创建预约"""
    userId: str
    userName: str
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    address: Optional[str] = None
    paymentMethod: Optional[str] = Field(None, pattern="^(captain|aa)$")
    teamId: Optional[str] = None
    teamName: Optional[str] = None
    matchId: Optional[str] = None
    position: Optional[str] = None
    participants: Optional[List[BookingParticipantBase]] = None


class BookingResponse(BookingBase):
    """预约响应"""
    id: str
    userId: str
    userName: str
    location: Location
    paymentMethod: Optional[str] = None
    teamId: Optional[str] = None
    teamName: Optional[str] = None
    matchId: Optional[str] = None
    position: Optional[str] = None
    status: str = "pending"
    createdAt: Optional[datetime] = None
    paidAt: Optional[datetime] = None
    cancelledAt: Optional[datetime] = None
    cancelReason: Optional[str] = None
    participants: List[BookingParticipantResponse] = []
    
    class Config:
        from_attributes = True


class OrderFilter(BaseModel):
    """订单筛选条件"""
    status: Optional[str] = Field(None, pattern="^(pending|paid|confirmed|finished|cancelled)$")
    type: Optional[str] = Field(None, pattern="^(team|match)$")
    startDate: Optional[str] = Field(None, pattern=r"^\d{4}-\d{2}-\d{2}$")
    endDate: Optional[str] = Field(None, pattern=r"^\d{4}-\d{2}-\d{2}$")

